package org.com.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.com.pojo.Farms;
import org.com.vo.FarmApplyVO;
import java.util.List;

/**
* @author 86134
* @description 针对表【farms】的数据库操作Mapper
* @createDate 2025-02-27 16:34:37
* @Entity org.com.pojo.Farms
*/
@Mapper
public interface FarmsMapper extends BaseMapper<Farms> {
   @Select("SELECT * FROM farms WHERE user_id = #{userId}")
   List<Farms> selectByUserId(@Param("userId") Integer userId);
   
   List<Farms> selectUserFarmsWithDetails(Integer userId);

   @Select("SELECT DISTINCT f.farm_id, f.farm_name, f.farm_address, f.applied_subsidy_amount, " +
          "f.created_at, f.status_id as status, " +
          "CASE WHEN f.status_id = 1 THEN '申请中' " +
          "     WHEN f.status_id = 3 THEN '已拒绝' " +
          "     WHEN f.status_id = 0 THEN '创办中' " +
          "     WHEN f.status_id = 2 THEN '营业中' " +
          "     ELSE '未知' END as status_text, " +
          "u.user_id, u.user_name, u.call_name, " +
          "(SELECT image_url FROM farms_images WHERE farm_id = f.farm_id AND is_primary = 1 LIMIT 1) as image_url " +
          "FROM farms f " +
          "LEFT JOIN users u ON f.user_id = u.user_id " +
          "ORDER BY f.created_at DESC")
   List<FarmApplyVO> getApplyFarms();

   @Update("UPDATE farms SET status_id = 1 WHERE farm_id = #{farmId} AND user_id = #{userId}")
   int reapplyFarm(@Param("farmId") Integer farmId, @Param("userId") Integer userId);
}




